package com.fx.single.config;
 
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @desc 数据源
 * @author dadovicn
 * @date   2019/1/4
 */
@Configuration
public class DruidDbConfig {
    private Logger logger = LoggerFactory.getLogger(DruidDbConfig.class);
 
    @Value("${spring.datasource.primary.url}")
    private String dbUrl1;
 
    @Value("${spring.datasource.primary.username}")
    private String username1;
 
    @Value("${spring.datasource.primary.password}")
    private String password1;
 
    @Value("${spring.datasource.primary.driver-class-name}")
    private String driverClassName;
 
    @Value("1")
    private int initialSize;
 
    @Value("5")
    private int minIdle;
 
    @Value("20")
    private int maxActive;
 
    @Value("60000")
    private int maxWait;

    /** 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒 */
    @Value("60000")
    private int timeBetweenEvictionRunsMillis;

    /** 配置一个连接在池中最小生存的时间，单位是毫秒 */
    @Value("300000")
    private int minEvictableIdleTimeMillis;
 
    @Value("SELECT 1 FROM DUAL")
    private String validationQuery;
 
    @Value("true")
    private boolean testWhileIdle;
 
    @Value("false")
    private boolean testOnBorrow;
 
    @Value("false")
    private boolean testOnReturn;

    /** 打开PSCache，并且指定每个连接上PSCache的大小 */
    @Value("true")
    private boolean poolPreparedStatements;
 
    @Value("20")
    private int maxPoolPreparedStatementPerConnectionSize;

    /** 配置监控统计拦截的filters，去掉后监控界面sql无法统计，'wall'用于防火墙 */
    @Value("stat, slf4j")
    private String filters;

    /** 通过connectProperties属性来打开mergeSql功能；慢SQL记录 */
    @Value("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500")
    private String connectionProperties;
 
    @Bean(name = "policeDataSource")
    @Qualifier("policeDataSource")
    public DataSource dataSource() {
        return getDruidDataSource(username1, password1, dbUrl1);
    }

    private DruidDataSource getDruidDataSource(String username, String password, String url) {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(url);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
            logger.error("druid configuration initialization filter : {0}", e);
        }
        datasource.setConnectionProperties(connectionProperties);
        return datasource;
    }

    @Bean
    public ServletRegistrationBean druidServlet() {
        logger.info("init Druid Servlet Configuration ");
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "admin");
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}
